package com.test.stuff;

import java.text.NumberFormat;


public class Account{
	
	protected String name;
	protected int id;
	protected double balance;
	
	protected static Logger logger = new Logger(Account.class.getSimpleName());
	
	//////
	// constructors
	
	public Account(String name, int id) throws Exception{
		this(name, id, 0);
	}

	public Account(String name, int id, double balance) throws Exception{
		if(name==null || name.isEmpty())
			throw new Exception("Cannot init Account because name is invalid");
		if(id<1)
			throw new Exception("Cannot init Account because id is invalid");
		
		this.name = name;
		this.id = id;
		this.balance = balance;
	}
	
	//////
	// overrides
	
	@Override
	public int hashCode(){
		int prime = 11;
		return (int) (Math.pow((id*prime), (name.length()))+balance);
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this==obj)
			return true;
		
		if(obj==null){
//	    	logger.log("obj null");
			return false;
		}
		
		if (this.getClass() != obj.getClass()) {
//	    	logger.log("classes() don't match");
//	    	logger.log(this.getClass()+" vs "+obj.getClass());
	        return false;
	    }
		
	    final Account other = (Account) obj;
	    // if this name is null, then the other name should be null
	    // otherwise check if the names are the same
	    if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
//	    	logger.log("name don't match");
//	    	logger.log(this.name+" vs "+other.name);
	        return false;
	    }
	    
	    // finally verify id is the same value
	    if (this.id != other.id) {
//	    	logger.log("id don't match");
//	    	logger.log(this.id+" vs "+other.id);
	        return false;
	    }
	    
	    // if name and id are the same, then they are equal
	    return true;
	}
	
	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();

		result.append("Account Details -- ");
		result.append("Name: "+this.name+", ");
		result.append("ID: "+this.id+", ");
//		DecimalFormat df = new DecimalFormat("#.##");
//		result.append("Account Balance: "+df.format(this.balance)+NEW_LINE);
		NumberFormat nf = NumberFormat.getCurrencyInstance();
		result.append("Balance: "+nf.format(this.balance));
		
		return result.toString();
	}
	
	//////
	// setters
	
	public void setName(String name){
		if(name==null || name.isEmpty()){
			return;
		}
		this.name = name;
	}
	
	public void setId(int id){
		if(id<1){
			return;
		}
		this.id = id;
	}
	
	public void setBalance(double balance){
		this.balance=balance;
	}
	
	//////
	// getters
	
	public String getName(){ return this.name; }
	public int getId(){ return this.id; }
	public double getBalance(){ return this.balance; }
	 
	//////
	// methods
}